<?php


namespace app\unify\model;



class Api extends Base
{
    const CREATED_TYPE_MANUAL = 0;
    const CREATED_TYPE_SYNC = 1;

    const CREATED_TYPE_LIST = [
        self::CREATED_TYPE_MANUAL => '手动',
        self::CREATED_TYPE_SYNC => '同步'
    ];

    const ANONYMOUS_TRUE = 1;
    const ANONYMOUS_FALSE = 0;
    const ANONYMOUS_LIST = [
        self::ANONYMOUS_TRUE => '是',
        self::ANONYMOUS_FALSE => '否',
    ];

    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;
    const STATUS_LIST = [
        self::STATUS_ENABLE => '有效',
        self::STATUS_DISABLE => '失效'
    ];

    const ALLOW_TRUE = 1;
    const ALLOW_FALSE = 0;
    const ALLOW_LIST = [
        self::ALLOW_FALSE => '否',
        self::ALLOW_TRUE => '是',
    ];


    public function getCreatedTypeNameAttr($value, $data)
    {
        return self::CREATED_TYPE_LIST[$data['created_type']] ?? "-";
    }

    public function getStatusNameAttr($value, $data)
    {
        return self::STATUS_LIST[$data['status']] ?? "-";
    }

    public function getAllowNameAttr($value, $data)
    {
        return self::ALLOW_LIST[$data['allow']] ?? '-';
    }

    public function getPlatformNameAttr($value, $data)
    {
        $platformValues = [];
        $platformKeys = explode(",", $data['platform']?:'');
        foreach ($platformKeys as $platformKey) {
            if (empty(self::PLATFORM_LIST[$platformKey])) {
                continue;
            }
            $platformValues[] = self::PLATFORM_LIST[$platformKey];
        }

        return implode("、", $platformValues);
    }

    public function getAnonymousNameAttr($value, $data)
    {
        return self::ANONYMOUS_LIST[$data['anonymous']] ?? '-';
    }

    public function auths()
    {
        return $this->belongsToMany(
            Auth::class,
            ApiAuth::class,
            'auth_id',
            'api_id'
        );
    }

    public function authIds()
    {
        return $this->hasMany(ApiAuth::class, 'api_id', 'id');
    }

    public function apiAuths()
    {
        return $this->hasMany(ApiAuth::class, 'api_id', 'id');
    }
}